﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ChatForum.Domain;

namespace ChatForum.DAL
{
    static class Extension
    {
        internal static System.Data.IDbCommand Delete<T>(this IDbCommand command, Guid entityId)
        {
            command.CommandType = CommandType.Text;
            if (typeof(T) == typeof(User))
                return DataMapper.UserDeleteCommand(command, entityId);
            else if (typeof(T) == typeof(Category))
                return DataMapper.CategoryDeleteCommand(command, entityId);
            else if (typeof(T) == typeof(Post))
                return DataMapper.PostDeleteCommand(command, entityId);
            else
                return command;
        }
        internal static System.Data.IDbCommand SelectAll<T>(this IDbCommand command)
        {
            command.CommandType = CommandType.Text;
            if (typeof(T) == typeof(User))
                return DataMapper.UserSelectAllCommand(command);
            else if (typeof(T) == typeof(Category))
                return DataMapper.CategorySelectAllCommand(command);
            else if (typeof(T) == typeof(Post))
                return DataMapper.PostSelectAllCommand(command);
            else
                return command;
        }
        internal static System.Data.IDbCommand SelectByID<T>(this IDbCommand command, Guid entityId)
        {
            command.CommandType = CommandType.Text;
            if (typeof(T) == typeof(User))
                return DataMapper.UserSelectByIDCommand(command, entityId);
            else if (typeof(T) == typeof(Category))
                return DataMapper.CategorySelectByIDCommand(command, entityId);
            else if (typeof(T) == typeof(Post))
                return DataMapper.PostSelectByIDCommand(command, entityId);
            else
                return command;
        }
        internal static System.Data.IDbCommand Insert<T>(this IDbCommand command, T entity)
        {
            command.CommandType = CommandType.Text;
            if (typeof(T) == typeof(User))
                return DataMapper.UserInsertCommand(command, entity as User);
            else if (typeof(T) == typeof(Category))
                return DataMapper.CategoryInsertCommand(command, entity as Category);
            else if (typeof(T) == typeof(Post))
                return DataMapper.PostInsertCommand(command, entity as Post);
            return command;
        }
        internal static System.Data.IDbCommand Modify<T>(this IDbCommand command, object entity)
        {
            if (typeof(T) == typeof(User))
                return DataMapper.UserModifyCommand(command, entity as User);
            else if (typeof(T) == typeof(Category))
                return DataMapper.CategoryModifyCommand(command, entity as Category);
            else if (typeof(T) == typeof(Post))
                return DataMapper.PostModifyCommand(command, entity as Post);
            return command;
        }

        internal static IEnumerable<T> ReadAll<T>(this IDataReader reader) where T : EntityBase
        {
            List<T> entityList = new List<T>();
            int rowCount = 0;
            while (reader.Read())
            {
                rowCount++;
                entityList.Add(Map<T>((IDataRecord)reader));
            }
            return entityList;
        }
        internal static T FirstOrDefault<T>(this IDataReader reader) where T : EntityBase
        {
            if (reader.Read())
                return Map<T>((IDataRecord)reader);
            else return null;
        }
        private static T Map<T>(IDataRecord record) where T : EntityBase
        {
            if (typeof(T) == typeof(User))
                return DataMapper.MapUser(record) as T;
            if (typeof(T) == typeof(Category))
                return DataMapper.MapCategory(record) as T;
            if (typeof(T) == typeof(Post))
                return DataMapper.MapPost(record) as T;
            else
                return null;
        }
    }
}
